package com.kinon.norogamilk.ui.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.kinon.norogamilk.application.MilkApp;
import com.kinon.norogamilk.bean.ComBean;
import com.kinon.norogamilk.util.MyFunc;
import com.kinon.norogamilk.util.SerialHelper;

import java.io.IOException;
import java.security.InvalidParameterException;

/**
 * Created by lhqq on 2017-12-12.
 */

public class BaseActivity extends Activity {

    public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
    protected static String TAG = "lh";
    protected static String sn = "";
    protected static SerialControl mSerCon;
    private static byte highByt = 0;
    protected SerialHelper SerCont = new SerialControl() {

    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        noBar();
        if (TextUtils.isEmpty(sn)) {
            sn = android.os.Build.SERIAL;
//            Log.e(TAG, "sn== "+sn );
        }
        if (mSerCon == null) {
            mSerCon = new SerialControl();
            OpenComPort(mSerCon);
        }

//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    protected void mToast(String msg) {
        Toast.makeText(MilkApp.getInstance(), msg,
                Toast.LENGTH_SHORT).show();
    }

    protected String sendString(int comandId) {
//        int i=0;
        highByt++;
        if (highByt >= 127) {
            highByt = 1;
        }
        byte byt[] = new byte[8];
        byt[0] = 'E';
        byt[1] = 'F';
        byt[2] = (byte) 203;
        byt[3] = highByt;   //帧编号低字节
        byt[4] = 0;   //帧编号高字节 高低字节其中一个要与上次不一致
        byt[5] = (byte) comandId; //命令
        byt[6] = 0;
        byt[7] = 0;
        return getSendHexRCR(byt);
    }

    protected String sendHeatString(int comandId, int switc) {
//        int i=0;
        highByt++;
        if (highByt >= 127) {
            highByt = 1;
        }
        byte byt[] = new byte[11];
        byt[0] = 'E';
        byt[1] = 'F';
        byt[2] = (byte) 203;
        byt[3] = highByt;   //帧编号低字节
        byt[4] = 0;   //帧编号高字节 高低字节其中一个要与上次不一致
        byt[5] = (byte) comandId; //命令
        byt[6] = 3;
        byt[7] = 0;
        byt[8] = (byte) switc;
        byt[9] = 3;    //注解：微波炉工作时间高位
        byt[10] = 0;    //注解：微波炉工作时间低位  时间高低位合并后所得的时间单位为：秒
        return getSendHexRCR(byt);
    }

    protected String sendString(String raod) {
        int raodNum = Integer.parseInt(raod, 16);
        long time = System.currentTimeMillis();
        String stron = String.valueOf(time);
        int on_id = Integer.valueOf(stron.substring(stron.length() - 2, stron.length()));
        highByt++;
        if (highByt >= 127) {
            highByt = 1;
        }
        byte byt[] = new byte[31];
        byt[0] = 'E';
        byt[1] = 'F';
        byt[2] = (byte) 203;
        byt[3] = highByt;   //帧编号低字节
        byt[4] = 0;   //帧编号高字节 高低字节其中一个要与上次不一致
        byt[5] = (byte) 0x34;
        byt[6] = (byte) 0x17;
        byt[7] = 0;
        byt[8] = (byte) raodNum; //货道号
        byt[9] = 0;
        byt[10] = 0;
        byt[11] = 0;
        byt[12] = (byte) 0x64;    //货道价格
        byt[13] = 0;
        byt[14] = 0;
        byt[15] = 0;
        byt[16] = 2;   //产品编号
        byt[17] = 0;
        byt[18] = 0;
        byt[19] = 0;
        byt[20] = 1; //支付类型(0-现金,1-卡,2-银行卡,3-闪付,4-支付宝,6-微信)
        byt[21] = 0;
        byt[22] = 0;
        byt[23] = 0;
        byt[24] = (byte) 0x56;  //交易编号
        byt[25] = (byte) 0x30;
        byt[26] = (byte) 0x24;
        byt[27] = (byte) 0x53;
        byt[28] = (byte) 0x30;
        byt[29] = (byte) 0x32;
        byt[30] = 0;


        return getSendHexRCR(byt);
    }

    private String getSendHexRCR(byte byt[]) {
        String hex = MyFunc.ByteArrToHex(byt);

        String strCrc = MyFunc.getCRC(byt);
        if (strCrc.length() < 4) {
            strCrc = "0" + strCrc;
        }
//        Log.e(TAG, "strCrc== "+strCrc );
//        Log.e(TAG, "send== "+hex.replace(" ","")+strCrc.substring(2,4)+strCrc.substring(0,2) );
        return hex.replace(" ", "") + strCrc.substring(2, 4) + strCrc.substring(0, 2);
    }

    protected void sendPortData(String sOut) {
        if (mSerCon != null) {
            mSerCon.sendHex(sOut);
        }
    }

    protected void OpenComPort(SerialHelper ComPort) {
        try {
            ComPort.open();
        } catch (SecurityException e) {
            mToast("打开串口失败:没有串口读/写权限!");
        } catch (IOException e) {
            mToast("打开串口失败:未知错误!");
        } catch (InvalidParameterException e) {
            mToast("打开串口失败:参数错误!");
        }
    }

    //设置自动发送数据
    protected void SetLoopData(String sLoopData) {
        mSerCon.setHexLoopData(sLoopData);
    }

    //设置自动发送的时间
    protected void SetiDelayTime(int sTime) {
        mSerCon.setiDelay(sTime);
    }

    //设置自动发送开关
    protected void SetAutoSend(boolean isAutoSend) {
        if (isAutoSend) {
            mSerCon.startSend();
        } else {
            mSerCon.stopSend();
        }
    }

    protected void onDataReceiv(ComBean ComRecData) {

    }

    private void noBar() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        int uiFlags = View.SYSTEM_UI_FLAG_FULLSCREEN    //全屏会显示导航栏
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  //隐藏导航栏
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  //隐藏导航栏布局
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//        uiFlags |= 0x00001000;
        getWindow().getDecorView().setSystemUiVisibility(uiFlags);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onAttachedToWindow() {
//        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        super.onAttachedToWindow();
    }

    private class SerialControl extends SerialHelper {

        public SerialControl() {
        }

        @Override
        protected void onDataReceived(final ComBean ComRecData) {
            //数据接收量大或接收时弹出软键盘，界面会卡顿,可能和6410的显示性能有关
            //直接刷新显示，接收数据量大时，卡顿明显，但接收与显示同步。
            //用线程定时刷新显示可以获得较流畅的显示效果，但是接收数据速度快于显示速度时，显示会滞后。
            //最终效果差不多-_-，线程定时刷新稍好一些。
//            DispQueue.AddQueue(ComRecData);//线程定时刷新显示(推荐)
//            String
//            Log.e(TAG, "Base[5]=="+ComRecData.getbRec()[5]);
            runOnUiThread(new Runnable()//直接刷新显示
            {
                public void run() {
                    onDataReceiv(ComRecData);
                }
            });
        }
    }


}
